package com.dlyk.task;

import com.dlyk.DlykServerApplication;
import com.dlyk.bean.TActivity;
import com.dlyk.bean.TDicType;
import com.dlyk.bean.TDicValue;
import com.dlyk.bean.TProduct;
import com.dlyk.mapper.TProductMapper;
import com.dlyk.result.DicEnum;
import com.dlyk.service.ActivityService;
import com.dlyk.service.DicTypeService;
import com.dlyk.service.ProductService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 定时任务调度
 */
@Component
@EnableScheduling  // 允许开启任务调度
@Slf4j
public class DataTask {

    @Resource
    private DicTypeService dicTypeService;

    @Resource
    private ProductService productService;

    @Resource
    private ActivityService activityService;

    // 设置任务每3秒调度一次
    @Scheduled(cron = "${project.task.cron}",zone = "Asia/Shanghai",timeUnit = TimeUnit.MILLISECONDS)
    public void task(){
        log.info("任务调度中：{}",new Date());
        List<TDicType> tDicTypes=dicTypeService.loadAllDicData();

        for (TDicType tDicType : tDicTypes) {
            String typeCode = tDicType.getTypeCode();
            List<TDicValue> dicValueList = tDicType.getDicValueList();
            DlykServerApplication.cacheMap.put(typeCode,dicValueList);
        }

        // 查询所有的在售产品
        List<TProduct> productList=productService.getOnSaleProduct();
        DlykServerApplication.cacheMap.put(DicEnum.PRODUCT.getCode(),productList);

        // 查询所有正在进行的活动
        List<TActivity> activityList =activityService.getOnActivity();
        DlykServerApplication.cacheMap.put(DicEnum.ACTIVITY.getCode(),activityList);

    }
}
